package com.bwhx.eruler.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Feedback;
import com.bwhx.eruler.bean.Notice;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Project;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.service.FeedbackService;
import com.bwhx.eruler.service.ProjectService;

@Deprecated 
@Controller
public class ProjectController extends BaseController {
	

	
	@Autowired
	ProjectService projectService;
	@ResponseBody			
	//@RequestMapping(value = "/page.projectList.ashx")
	 public Map staffList(Integer page,Integer rows,Project project, @RequestBody String b) {
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,project);
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			List<Project> list = projectService.list(p);
			Integer i  = projectService.count(project);


			map.put("total",i );//total键 存放总记录数，必须的  A
			map.put("rows",list);//rows键 存放每页记录 list 
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	
	@ResponseBody			
	//@RequestMapping(value = "/page.projectListAll.ashx")
	 public List<Project> staffList() {
		//每页的开始记录
     
        Page p = new Page(0,0,new Project());
		List<Project> list = projectService.list(p);
	    return list;  
	  }
	
	
	@ResponseBody
	//@RequestMapping(value = "/page.saveOrUpdateProject.ashx")
	public String saveStaff(Project project,HttpSession session) {
		
		if(project.getPid()==null){
			
			List<Project> findByPcode = projectService.findByPcode(project);
			
			if(findByPcode.size()>0){
				return "pcodeconflict";
			}
			
			Staff staff = (Staff)session.getAttribute("staff");
			String name = staff.getsName();
			project.setCreator(name);
			project.setCreatetime(new Date());

			projectService.save(project);
		}else{
			
			List<Project> findByPcode = projectService.findByPcode(project);
			
			if(findByPcode.size()==1&&findByPcode.get(0).getPid()!=project.getPid()){
				return "pcodeconflict";
				
			}else if(findByPcode.size()>1){
				return "pcodeconflict";
			}
			
			projectService.update(project);
		}
	

		
	    return "success";  
	  }
	
	 
	
	
	
	

}
